<?php
namespace app\admin\command\statistics;

use app\admin\model\user\User;
use app\admin\model\user\Order;
use app\admin\model\agent\Agent;
use app\admin\model\tongji\StatisticsAgent;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class AgentStatistics extends Command{
    protected function configure()
    {
        $this->setName('day:agent')->setDescription('代理每日统计');
    }
    protected function execute(Input $input, Output $output)
    {
        $user_model=new User();
        $order_model=new Order();
        $agent_model=new Agent();
//        $agent_model=new StatisticsAgent();
        $statictics_day_model=new StatisticsAgent();

        $agent_list=$agent_model->select()->toArray();dump($agent_list);
        //统计每个代理数据
        foreach ($agent_list as $k=>$v){

            $user_num_agent=$user_model->whereTime('create_time','yesterday')->where('agent_id',$v['id'])->count();
            $ip_num_agent=$user_model->whereTime('create_time','yesterday')->where('agent_id',$v['id'])->group('ip')->count();
            $order_user_num_agent=$order_model->whereTime('create_time','yesterday')->where('status',1)->where('agent_id',$v['id'])->group('user_id')->count();
            $order_price_agent=$order_model->whereTime('create_time','yesterday')->where('status',1)->where('agent_id',$v['id'])->sum('price');
            $data[]=[
                'date'=>date('Ymd',strtotime('-1 day')),
                'agent_name'=>$v['agent_name'],
                'day_user'=>$user_num_agent,
                'day_ip_num'=>$ip_num_agent,
//                'hy_user_num'=>0,
                'day_rechage_num'=>$order_user_num_agent,
                'day_price'=>$order_price_agent,
                'agent_id'=>$v['id'],
                'p_agent_id'=>$v['p_agent_id'],
            ];
        }
        $statictics_day_model->saveAll($data);
        $user_num=$user_model->whereTime('create_time','yesterday')->count();
        $ip_num=$user_model->whereTime('create_time','yesterday')->group('ip')->count();
        $order_user_num=$order_model->whereTime('create_time','yesterday')->where('status',1)->group('user_id')->count();
        $order_price=$order_model->whereTime('create_time','yesterday')->where('status',1)->sum('price');
        $old_date=$statictics_day_model->where('date',date('Ymd',strtotime('-1 day')))->find();

        //记录总数据
        if(!empty($old_date)){
            $old_date->agent_name='总代理';
            $old_date->day_user=$user_num;
            $old_date->day_ip_num=$ip_num;
//            $old_date->hy_user_num=0;
            $old_date->day_rechage_num=$order_user_num;
            $old_date->day_price=$order_price;
            $old_date->save();
        }else{
            $data=[
                'date'=>date('Ymd',strtotime('-1 day')),
                'agent_name'=>'总代理',
                'day_user'=>$user_num,
                'day_ip_num'=>$ip_num,
//                'hy_user_num'=>0,
                'day_rechage_num'=>$order_user_num,
                'day_price'=>$order_price,
            ];
            $statictics_day_model->save($data);
        }
    }
}
